﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Cips.DataMining.IO
{
	/// <summary>
	/// Provides facilities for locating project directories.
	/// </summary>
	public static class Project
	{
		/// <summary>
		/// Finds the named project folder.
		/// </summary>
		/// <param name="name">
		/// The folder name.
		/// </param>
		/// <returns>
		/// Path the the asked folder, null if not found.
		/// </returns>
		public static string GetDirectory(string name)
		{
			var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

			do
			{
				if (path == null)
					return null;

				if (Directory.GetDirectories(path, name).Count() == 1)
					return Directory.GetDirectories(path, name).First();

				path = Path.GetFullPath(Path.Combine(path, ".."));
			}
			while (!String.IsNullOrEmpty(Path.GetDirectoryName(path)));

			return null;
		}

		/// <summary>
		/// Finds the named test folder.
		/// </summary>
		/// <param name="name">
		/// The folder name.
		/// </param>
		/// <returns>
		/// Path the the asked folder, null if not found.
		/// </returns>
		public static string GetTestDirectory(string name)
		{
			var retval = GetDirectory("Test");

			if (retval != null)
			{
				retval = Path.Combine(retval, name);

				if (Directory.Exists(retval))
					return retval;
			}

			return null;
		}
	}
}
